home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / satellit / vstsrc / select.c < prev    next >
C/C++ Source or Header  |  1995-02-02  |  19KB  |  576 lines

  1. /*
  2.  * %W% %E% %U%  [EXTREL_1.2]
  3.  *
  4.  * VersaTrack orbit calculations are based on those that appear in Dr. Manfred
  5.  * Bester's sattrack program (the Unix(tm) versions 1 and 2).
  6.  *
  7.  * The data from which the maps where generated come from "xsat", an
  8.  * X-Windows program by David A. Curry (N9MSW).
  9.  *
  10.  * Site coordinates come from various sources, including a couple of
  11.  * World Almanacs, and also from both of the programs mentioned above.
  12.  *
  13.  * The following are authors' applicable copyright notices:
  14.  *
  15.  *                                                                               
  16.  * Copyright (c) 1992, 1993, 1994 Manfred Bester. All Rights Reserved.        
  17.  *                                                                           
  18.  * Permission to use, copy, modify, and distribute this software and its      
  19.  * documentation for educational, research and non-profit purposes, without   
  20.  * fee, and without a written agreement is hereby granted, provided that the  
  21.  * above copyright notice and the following three paragraphs appear in all    
  22.  * copies.                                                                    
  23.  *                                                                              
  24.  * Permission to incorporate this software into commercial products may be    
  25.  * obtained from the author, Dr. Manfred Bester, 1636 M. L. King Jr. Way,     
  26.  * Berkeley, CA 94709, USA.                                                   
  27.  *                                                                             
  28.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,  
  29.  * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF    
  30.  * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN ADVISED   
  31.  * OF THE POSSIBILITY OF SUCH DAMAGE.                                         
  32.  *                                                                             
  33.  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT       
  34.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A    
  35.  * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"       
  36.  * BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,  
  37.  * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.                                   
  38.  *                                                                             
  39.  *                                                                             
  40.  * Copyright 1992 by David A. Curry                                            
  41.  *                                                                             
  42.  * Permission to use, copy, modify, distribute, and sell this software and its 
  43.  * documentation for any purpose is hereby granted without fee, provided that  
  44.  * the above copyright notice appear in all copies and that both that copyright
  45.  * notice and this permission notice appear in supporting documentation.  The  
  46.  * author makes no representations about the suitability of this software for  
  47.  * any purpose.  It is provided "as is" without express or implied warranty.   
  48.  *                                                                             
  49.  * David A. Curry, N9MSW                                                       
  50.  * Purdue University                                                           
  51.  * Engineering Computer Network                                                
  52.  * 1285 Electrical Engineering Building                                        
  53.  * West Lafayette, IN 47907                                                    
  54.  * davy@ecn.purdue.edu                                                         
  55.  *                                                                             
  56.  * VersaTrack Copyright (c) 1993, 1994 Siamack Navabpour. All Rights Reserved.
  57.  *
  58.  * Permission is hereby granted to copy, modify and distribute VersaTrack
  59.  * in whole, or in part, for educational, non-profit and non-commercial use
  60.  * only, free of charge or obligation, and without agreement, provided that
  61.  * all copyrights and restrictions noted herein are observed and followed, and
  62.  * additionally, that this and all other copyright notices listed herein
  63.  * appear unaltered in all copies and in all derived work.
  64.  *
  65.  * This notice shall not in any way void or supersede any of the other authors
  66.  * rights or privileges.
  67.  *
  68.  * VersaTrack IS PRESENTED FREE AND "AS IS", WITHOUT ANY WARRANTY OR SUPPORT.
  69.  * YOU USE IT AT YOUR OWN RISK. The author(s) shall not be liable for any
  70.  * direct, indirect, incidental, or consequential damage, loss of profits or
  71.  * other tangible or intangible losses or benefits, arising out of or related
  72.  * to its use. VersaTrack carries no warranty, explicit or implied, including
  73.  * but not limited to those of merchantablity and fitness for a particular
  74.  * purpose.
  75.  *
  76.  * Siamack Navabpour, 12342 Hunter's Chase Dr. Apt. 2114, Austin, TX 78729.
  77.  * sia@bga.com or sia@realtime.com.
  78.  */
  79.  
  80.  
  81. #include <windows.h>
  82. #include <stdio.h>
  83. #include <stdlib.h>
  84. #include <math.h>
  85.  
  86. #include "resource.h"
  87. #include "vstdefs.h"
  88. #include "constant.h"
  89. #include "vsttype.h"
  90. #include "vstextrn.h"
  91.  
  92. #define DEFFLAGS    (SE_RTD_DIC|SE_GTR_UTC|SE_RTD_UTC|SE_RTD_POA|SE_RTD_BOA)
  93. extern void flagStr(unsigned int, char *);
  94.  
  95. struct flags_t {
  96.     char *name;
  97.     unsigned int orvalue, andvalue;
  98. };
  99.  
  100. static struct flags_t Flagtab[16] = {
  101.     "BOA", SE_RTD_BOA, 0,
  102.     "POA", SE_RTD_POA, 0,
  103.     "BE", SE_RTD_BEEP, 0,
  104.     "ICO", SE_RTD_DIC, 0,
  105.     "BMP", SE_RTD_BMP, 0,
  106.     "GU", SE_GTR_UTC,  SE_GTR_LOC | SE_GTR_SLT,
  107.     "GL", SE_GTR_LOC,  SE_GTR_UTC | SE_GTR_SLT,
  108.     "GS", SE_GTR_SLT,  SE_GTR_UTC | SE_GTR_LOC,
  109.     "RU", SE_RTD_UTC,  SE_RTD_LOC | SE_RTD_SLT,
  110.     "RL", SE_RTD_LOC,  SE_RTD_UTC | SE_RTD_SLT,
  111.     "RS", SE_RTD_SLT,  SE_RTD_UTC | SE_RTD_LOC,
  112.     "HIC",SE_RTD_HIC,  SE_RTD_DIC,
  113.     "SIM", SE_RTD_SIM, 0,
  114.     "UA", SE_RTD_USERA, 0,
  115.     "UB", SE_RTD_USERB, 0,
  116.     0, 0, 0
  117. };
  118.  
  119.  
  120. /*
  121.  * List the active list.
  122.  */
  123.  
  124. void
  125. listSelections(hwnd)
  126. HWND hwnd; 
  127. {
  128.     register select_t *sp;
  129.  
  130.     for (sp = selInfo; sp ; sp = sp->sl_next)
  131.         sp->sl_index =  SendMessage(hwnd, LB_ADDSTRING, (WPARAM)0, (LPARAM) sp->sl_buf);
  132. }
  133.  
  134.  
  135. select_t *
  136. addSelection(satp, sitep, start, dur, step, minelev, update, flags)
  137. satellite_t *satp;
  138. site_t *sitep;
  139. double start, dur, step, minelev;
  140. int update;
  141. unsigned int flags;
  142. {
  143.     select_t *sp,*spp;
  144.     track_t *tp;
  145.     
  146.     if (!satp || !sitep) {
  147.         usermsg("must select a satellite and site first!");
  148.         return NULL;
  149.     }
  150.  
  151.     if ((sp = findSelectionbyName(satp, sitep)) != NULL) {
  152.         usermsg("Duplicate entry already exists.");
  153.         return NULL;
  154.     }
  155.  
  156.     spp = (select_t *) safeAlloc(sizeof(select_t));
  157.     tp = (track_t *) safeAlloc(sizeof(track_t));
  158.  
  159.     if (!spp || !tp)
  160.         return NULL;
  161.  
  162.     if (!(spp->sl_rp = (result_t *) safeAlloc(sizeof(result_t))))
  163.         return NULL;
  164.     
  165.     InitializeCriticalSection(&spp->rtd_mutex);
  166.     
  167.     tp->satp = satp;
  168.     tp->sitep = sitep;
  169.  
  170.     tp->steptime = step;
  171.     tp->minelevation = (float)minelev;
  172.     tp->starttime = start;
  173.     tp->duration = dur;
  174.     spp->sl_tp = tp;
  175.     spp->rtdhwnd = 0;
  176.     spp->rtd_thread_handle = NULL;
  177.     spp->sl_index = -1;
  178.     spp->flags = flags ? flags : DEFFLAGS;
  179.     spp->updatetime = update ? update : 30;
  180.     spp->sl_next = NULL;
  181.  
  182.     sprintf(tmpbuf, "%-14.14s %-.31s, %-.39s", satp->s_name, sitep->c_name, sitep->c_locale);
  183.     strncpy(spp->sl_buf, tmpbuf, sizeof(spp->sl_buf));
  184.  
  185.     if (selInfo == NULL)
  186.         selInfo = spp;
  187.     else {
  188.         for (sp = selInfo; sp->sl_next; sp = sp->sl_next) ;
  189.         sp->sl_next = spp;
  190.     }
  191.     return spp;
  192. }
  193.  
  194. BOOL
  195. modifySelectionbyName(hwnd, selp, satp, sitep, start, dur, step, minelev, update, flags)
  196. HWND hwnd;
  197. select_t *selp;
  198. satellite_t *satp;
  199. site_t *sitep;
  200. double start, dur, step, minelev;
  201. int update;
  202. unsigned int flags;
  203. {
  204.     track_t *tp;
  205.     
  206.     if (!selp)
  207.         return FALSE;
  208.  
  209.  
  210.     if (selp->running || selp->rtdhwnd) {
  211. #ifdef _DEBUG_
  212.         diag("modifySelection: rtd running: %d  hwnd %x\n", selp->running, selp->rtdhwnd);
  213. #endif /* _DEBUG_ */
  214.         usermsg("Entry currently in use. Cannot modify.");
  215.         return FALSE;
  216.     }
  217.     if (!satp || !sitep) {
  218.         usermsg("must select satellite and site first!");
  219.         return FALSE;
  220.     }
  221.     tp = selp->sl_tp;
  222.  
  223.     tp->satp = satp;
  224.     tp->sitep = sitep;
  225.     
  226.     tp->starttime = start;
  227.     tp->duration = (dur <= 0.0) ? atof(DURATION_DEFAULT) : dur;
  228.     tp->steptime = (step <= 0.0) ? atof(STEPTIME_DEFAULT)/SPD : step;
  229.     selp->updatetime = (update <=0 ) ? atoi(UPDATE_DEFAULT) : update;
  230.     tp->minelevation = (float) minelev;
  231.     selp->flags = flags;
  232.  
  233.     sprintf(tmpbuf, "%-14.14s %-.31s, %-.39s", satp->s_name, sitep->c_name, sitep->c_locale);
  234.     strncpy(selp->sl_buf, tmpbuf, sizeof(selp->sl_buf));
  235.  
  236.     SendMessage(hwnd, LB_DELETESTRING, (WPARAM) selp->sl_index, (LPARAM) 0);
  237.     SendMessage(hwnd, LB_INSERTSTRING, (WPARAM) selp->sl_index, (LPARAM) selp->sl_buf);
  238.  
  239.     return TRUE;
  240. }
  241.  
  242.  
  243. int
  244. deleteSelection(select_t * sp)
  245. {
  246.     select_t *spp, *psp;
  247.     int index;
  248.     track_t *tp;
  249.     
  250.     if (selInfo == NULL) {
  251.         usermsg("Active list is empty");
  252.         return -1;
  253.     }
  254.     if (sp == NULL) {
  255.         usermsg("No current selection");
  256.         return -1;
  257.     }
  258.     for (psp = NULL, spp = selInfo; spp ; spp = spp->sl_next) {
  259.         if (spp == sp) {
  260.             if (spp->running || spp->rtdhwnd) {
  261.                 tp = spp->sl_tp;
  262.                 sprintf(tmpbuf,"Entry for (%-.31s , %-.39s) is currently in use.",
  263.                     tp->satp->s_name, tp->sitep->c_name);
  264.                 usermsg(tmpbuf);
  265.             }
  266.             sp = spp->sl_next;
  267.             index = spp->sl_index;
  268.             if (psp) {
  269.                 psp->sl_next = sp;
  270.             }
  271.             else
  272.                 selInfo = sp;
  273.                 
  274.             if (spp == selInfo)
  275.                 selInfo = NULL;
  276.            
  277.             if (currentSel == spp)
  278.                 currentSel  = NULL;
  279.                 
  280.             for (; sp ; sp = sp->sl_next)
  281.                 sp->sl_index -= 1;
  282.                 
  283.             safeFree(spp->sl_tp);
  284.             safeFree(spp->sl_rp);
  285.                         
  286.             DeleteCriticalSection(&spp->rtd_mutex);
  287.             safeFree(spp);
  288.             
  289.             return index;
  290.         }
  291.         psp = spp;
  292.     }
  293.     usermsg("Entry not found");
  294.     return -1;
  295. }
  296.  
  297.  
  298. select_t *
  299. findSelectionbyName(satp, sitep)
  300. satellite_t *satp;
  301. site_t *sitep;
  302. {
  303.     select_t *sp;
  304.     register track_t *tp;
  305.     
  306.     if (satp && sitep) {
  307.         for (sp = selInfo; sp ; sp = sp->sl_next) {
  308.             tp = sp->sl_tp;
  309.         if (satp == tp->satp && sitep == tp->sitep) {
  310.             if ((sp != currentSel) && ((serverInfo[0].srv_state == ST_SRV_RUNNING)||
  311.                 (serverInfo[1].srv_state == ST_SRV_RUNNING)))
  312.                 if (!yesno("Radio/Rotator Control running! Are you sure you want \
  313. to change the current satellite ?"))
  314.                         return NULL;
  315.                 return currentSel = sp;
  316.             }
  317.         }
  318.     }
  319.     return (select_t *)NULL;
  320. }
  321.  
  322. select_t *
  323. findSelectionbyID(id)
  324. register int id;
  325. {
  326.     register select_t *sp;
  327.     
  328.     for (sp = selInfo; sp ; sp = sp->sl_next)
  329.         if (sp->sl_index == id) {
  330.             if ((sp != currentSel) && ((serverInfo[0].srv_state == ST_SRV_RUNNING)||
  331.                 (serverInfo[1].srv_state == ST_SRV_RUNNING)))
  332.                 if (!yesno("Radio/Rotator Control running! Are you sure you want \
  333. to change the current satellite ?"))
  334.                         return NULL;
  335.             return currentSel = sp;
  336.         }
  337.     return (select_t *) NULL;
  338. }
  339.  
  340.  
  341. /*
  342.  * convert flag bits to comma separated list of character "abreviations".
  343.  */ 
  344. void
  345. flagStr(flags, str)
  346. unsigned int flags;
  347. char *str;
  348. {
  349.     struct flags_t *fsp;
  350.     
  351.     str[0] = 0;
  352.     for (fsp = Flagtab; fsp->name; fsp++) {
  353.         if (flags & fsp->orvalue) {
  354.             if (str[0])
  355.                 strcat(str, ",");
  356.             strcat(str, fsp->name);
  357.         }
  358.     }
  359. }
  360.  
  361. /*
  362.  *  Read the active list from active.dat.
  363.  */
  364.  
  365. void readactiveDB()
  366. {    
  367.     char *sitenamep, *satnamep, *cp;
  368.     char *line, linebuf[240];
  369.     double minelev, steptime, duration, ttime;
  370.     char *delim = " \t\n;,";
  371.     FILE *fp;
  372.     int lineno, update;
  373.     unsigned int flags;
  374.     site_t *sitep;
  375.     satellite_t *satp;
  376.     struct flags_t *flp;
  377.     extern satellite_t *findSatbyName(char *);
  378.     
  379.     sprintf(linebuf,"%s\\%s", datadir, activefilename);
  380.     if ((fp = fopen(linebuf, "r")) == NULL) {
  381.         sprintf(tmpbuf,"Can't open %s", linebuf);
  382. #ifdef _DEBUG_
  383.         diag(tmpbuf);
  384. #endif /* _DEBUG_ */
  385.         usermsg(tmpbuf);
  386.     }
  387.  
  388.     lineno = 0;
  389.     if (!timeZoneName[0]) {
  390.         strcpy(timeZoneName,"UTC");
  391.         timeZone = 0.0;
  392.     }
  393. #ifdef _DEBUG_
  394.     diag("Active list:\n");
  395. #endif /* _DEBUG_ */
  396.  
  397.     while (fp && !feof(fp)) {
  398.         linebuf[0] = 0;
  399.         if (!(line = getline(linebuf, sizeof(linebuf), fp)) || (linebuf[0] == 0))
  400.             break;
  401.         lineno++;
  402.         if (line[0] == '#' || line[0] == '\n')
  403.             continue;
  404.             
  405.         satnamep = strtok(line, ":");
  406.  
  407.         sitenamep = strtok(NULL, ":");
  408.         sitenamep = stripLeadingSpace(sitenamep);
  409.  
  410.         if (sitenamep) {
  411.             stripTrailingSpace(sitenamep);
  412.             if (strchr(sitenamep,'*'))
  413.                 sitenamep = SiteName;
  414.         }
  415.         else
  416.             sitenamep = "???";
  417.             
  418.         if (satnamep)
  419.             stripTrailingSpace(satnamep);
  420.         else
  421.             satnamep = "???";
  422.  
  423.         if ((satp =  findSatbyName(satnamep)) == NULL) {
  424.             sprintf(tmpbuf, "%s line %d: can't find satellite \"%s\".",
  425.                 activefilename, lineno, satnamep);
  426.             usermsg(tmpbuf);
  427.             continue;
  428.         }
  429.  
  430.         if ((sitep = findSitebyName(sitenamep)) == NULL) {
  431.             sprintf(tmpbuf, "%s line %d: can't find site \"%s\".",
  432.                 activefilename, lineno, sitenamep);
  433.             usermsg(tmpbuf);
  434.             continue;
  435.         }
  436.         
  437.         ttime = -1.0;
  438.         cp = strtok(NULL, ",;!|");
  439.         if (cp) {
  440.             cp = stripLeadingSpace(cp);
  441.             stripTrailingSpace(cp);
  442.             if (*cp != '*') {
  443.                 ttime = dateStrtoTime(cp);
  444.                 if (ttime < 0.0) {
  445.                     sprintf(tmpbuf,"Invalid date/time format in %s line %d\n",
  446.                         activefilename, lineno);
  447.                     usermsg(tmpbuf);
  448.                     ttime = -1.0;
  449.                 }
  450.             }
  451.         }
  452.         cp = strtok(NULL, delim);
  453.         cp = stripLeadingSpace(cp);
  454.         duration = atof((cp && *cp != '*') ? cp : DURATION_DEFAULT);
  455.  
  456.         cp = strtok(NULL, delim);
  457.         cp = stripLeadingSpace(cp);
  458.         steptime = atof((cp && *cp != '*') ? cp : STEPTIME_DEFAULT) / SPD;
  459.  
  460.         cp = strtok(NULL, delim);
  461.         cp = stripLeadingSpace(cp);
  462.         minelev = atof((cp && *cp != '*') ? cp : MINELEVATION_DEFAULT ) * CDR;
  463.  
  464.         cp = strtok(NULL, delim);
  465.         cp = stripLeadingSpace(cp);
  466.         update = atoi((cp && *cp != '*') ? cp :  UPDATE_DEFAULT);
  467.  
  468.         cp = strtok(NULL, delim);
  469.         flags = SE_RTD_KM;
  470.         while (cp) {
  471.             cp = stripLeadingSpace(cp);
  472.             for (flp = Flagtab; flp->name; flp++) {
  473.                 if (cistrcmp(cp, flp->name) == 0) {
  474.                     flags &= ~ flp->andvalue;
  475.                     flags |= flp->orvalue;
  476.                 }
  477.             }
  478.             cp = strtok(NULL, delim);
  479.         } while (cp);
  480. #ifdef _DEBUG_
  481.         diag("%02d: %s, %s\n", lineno, satp->s_name, sitep->c_name);
  482. #endif /* _DEBUG_ */
  483.         addSelection(satp, sitep, ttime, duration, steptime, minelev, update, flags);
  484.     }
  485.  
  486.     if (selInfo)
  487.         currentSel = selInfo;
  488.     if (fp)
  489.         fclose(fp);
  490. }
  491.  
  492. void
  493. saveactivelist()
  494. {
  495.     FILE *fp;
  496.     select_t *selp;
  497.     track_t *tp;
  498.     int lineno, i;
  499.     char *sitenamep;
  500.     char start[40], step[10], dur[10], minelev[10], update[10];
  501.  
  502.     if (!selInfo)
  503.         return;
  504.  
  505.     sprintf(tmpbuf,"%s\\%s", datadir,activefilename);
  506.  
  507.     if ((fp = fopen(tmpbuf,"w")) == NULL) {
  508.         sprintf(string,"Cannot create/write \"%s\"\n",tmpbuf);
  509. #ifdef _DEBUG_
  510.         diag(string);
  511. #endif /* _DEBUG_ */
  512.         usermsg(string);
  513.         return;
  514.     }
  515.  
  516.     fprintf(fp,"#TZNAME= %s, TZDIFF= %-.3f\n", timeZoneName, timeZone * 24.0);
  517.     fprintf(fp,"#\n# Satellite, site,start time, duration, step time, min. elev., update time, flags\n");
  518.  
  519.     for (lineno = 4, selp = selInfo; selp; selp = selp->sl_next, lineno++) {
  520.         tp = selp->sl_tp;
  521.  
  522.         if (selp->updatetime == 30)
  523.             strcpy(update, "*");
  524.         else
  525.             sprintf(update,"%d", selp->updatetime);
  526.  
  527.         if (tp->starttime == -1.0)
  528.             strcpy(start,"*");
  529.         else
  530.             dateStr(tp->starttime, tp->sitep, SE_GTR_UTC, 0, start);
  531.  
  532.         if (tp->steptime > (29.0/SPD) && tp->steptime < (31.0/SPD) )
  533.             strcpy(step,"*");
  534.         else
  535.             sprintf(step,"%.0f", tp->steptime * SPD);
  536.  
  537.         if (tp->duration == 1.0)
  538.             strcpy(dur, "*");
  539.         else
  540.             sprintf(dur,"%.4f", tp->duration);
  541.  
  542.         if (tp->minelevation > (-0.2*CDR) && tp->minelevation < (0.2*CDR))
  543.             strcpy(minelev, "*");
  544.         else
  545.             sprintf(minelev, "%.0f", tp->minelevation * CRD);
  546.  
  547.         sprintf(tmpbuf, "%s, %s", tp->sitep->c_name, tp->sitep->c_locale);
  548.         sitenamep = tmpbuf;
  549.         if (cistrcmp(sitenamep, SiteName) == 0)
  550.             sitenamep = "*";
  551.  
  552.         for (i=0; i<BUFLEN; i++) textbuf[i] = 0;
  553.         sprintf(textbuf, "%-.31s: %-.39s:", tp->satp->s_name, sitenamep);
  554.         for (i=strlen(textbuf); i<30; i++) textbuf[i] = ' ';
  555.  
  556.         flagStr(selp->flags, tmpbuf);
  557.         fprintf(fp, "%s  %s,\t%s\t%s\t%s\t%s\t%s\n", textbuf,  start, dur, step,
  558.             minelev, update, tmpbuf);
  559.         fflush(fp);
  560.  
  561.         if (ferror(fp))
  562.             break;
  563.     }
  564.  
  565.     if (ferror(fp))
  566.         usermsg("Error writing selections to file");
  567.  
  568.     fclose(fp);
  569.  
  570.     sprintf(tmpbuf, "%d entries in active list", lineno-4);
  571.     usermsg(tmpbuf);
  572. #ifdef _DEBUG_
  573.     diag("wrote %d entries to defaults file\n", lineno-4);
  574. #endif
  575. }
  576.